#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QImage>
#include <QPicture>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //QPixmap做绘图设备, 对不同的平台做了显示优化
//    QPixmap pixmap(300, 300);

//    pixmap.fill(Qt::white);

//    QPainter painter(&pixmap);

//    painter.setPen(QPen(Qt::red));
//    painter.drawEllipse(QPoint(150, 150), 100, 100);

//    pixmap.save("C:/chenpeng/pix.png");

    //QImage做绘图设备， 不同平台下显示效果一样，可以对像素的访问做了优化
//    QImage image(300, 300, QImage::Format_RGB32);

//    image.fill(Qt::white);

//    QPainter painter(&image);

//    painter.setPen(QPen(Qt::red));
//    painter.drawEllipse(QPoint(150, 150), 100, 100);

//    image.save("C:/chenpeng/image.png");

    //QPicture 记录和重现绘图指令

    QPicture pic;
    QPainter painter;
    painter.begin(&pic);

    painter.setPen(QPen(Qt::blue));
    painter.drawEllipse(QPoint(150, 150), 100, 100);

    painter.end();

    //后缀名随意
    pic.save("C:/chenpeng/pic.zt");


}

void Widget::paintEvent(QPaintEvent *){
//    QImage image;
//    image.load(":/resources/1002.jpg");

//    QPainter painter(this);

//    //修改像素点
//    for(int i = 200; i < 400; i++){
//        for(int j = 200; j < 400; j++){
//            QRgb value = qRgb(255, 0, 0);
//            image.setPixel(i, j, value);
//        }
//    }

//    painter.drawImage(0, 0, image);


    //重现QPicture命令
    QPainter painter(this);
    QPicture pic;
    pic.load("C:/chenpeng/pic.zt");
    painter.drawPicture(0, 0, pic);

}

Widget::~Widget()
{
    delete ui;
}
